package org.luxor.commons.log.filter;

import org.luxor.commons.log.component.HttpRequestWrapper;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.yan @date 2019/10/12
 */
public class RequestLogFilter extends OncePerRequestFilter {

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (RestLog.isDebugEnabled()) {
            HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
            RestLog.logRequest(requestWrapper);
            filterChain.doFilter(requestWrapper, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }


}